package org.aeon.gamechatnest.mapper;

import org.aeon.gamechatnest.pojo.FriendRequest;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author 89342
 * @description 针对表【friend_request(好友请求表)】的数据库操作Mapper
 * @createDate 2024-05-09 09:25:50
 * @Entity org.aeon.gamechatnest.pojo.FriendRequest
 */
@Mapper
public interface FriendRequestMapper {
    /**
     * 添加
     *
     * @param friendRequest 好友请求新增实体
     * @return 影响行数
     */
    int add(FriendRequest friendRequest);

    /**
     * 根据好友请求编号id查询一个请求
     *
     * @param id id
     * @return 好友请求实体
     */
    @Select("""
            select request_id, from_user, to_user, create_time, verify_content, verify_status
            from friend_request
            where request_id = #{id}
            """)
    FriendRequest getById(Long id);

    /**
     * 获取双方之间存在的一个好友请求
     *
     * @param fromId 发起用户id
     * @param toId   接受用户id
     * @return 好友请求实体
     */
    @Select("""
            select request_id, from_user, to_user, create_time, verify_content, verify_status
            from friend_request
            where (from_user = #{fromId} and to_user=#{toId})
            or (from_user = #{toId} and to_user=#{fromId})
            """)
    FriendRequest getByBothSides(Long fromId, Long toId);

    /**
     * 查询某用户收到的的请求（只查未通过）
     */
    @Select("""
            select request_id, from_user, to_user, create_time, verify_content, verify_status
            from friend_request
            where to_user = #{toUser} and verify_status=0
            """)
    List<FriendRequest> getUnPassByToUser(Long toUser);

    /**
     * 更新状态
     *
     * @param id           好友请求编号id
     * @param verifyStatus 验证状态（0：未通过，1：接受，2：拒绝）
     * @return 影响行数
     */
    @Update("update friend_request set verify_status=#{verifyStatus} where request_id=#{id}")
    int updateVerifyStatus(Long id, Integer verifyStatus);

    /**
     * 通过双方id删除请求
     *
     * @param userId   用户id
     * @param friendId 好友id
     * @return 影响行数
     */
    @Delete("""
            delete from friend_request
            where (from_user = #{userId} and to_user = #{friendId})
                or (to_user = #{userId} and from_user = #{friendId})
            """)
    int deleteByBothSides(Long userId, Long friendId);

    /**
     * 通过id删除请求
     *
     * @param id id
     * @return 影响行数
     */
    @Delete("""
            delete from friend_request
            where request_id = #{id}
            """)
    int deleteById(Long id);
}




